import ApiV1WxMessagePageList_post from "@/api/ApiV1WxMessagePageList_post";
import icon from "@/assets/images/logo-universal.png";
import { IRawMsg } from "@/chatlog_types/IRawMsg";
import { IRawMsgContent_File, IRawMsgContent_Image, IRawMsgContent_Link } from "@/chatlog_types/IRawMsgContents";
import { Chat } from "@/views/Chat";
import { RawMsgType } from "@/views/ChatView/RawMsgType";
import dayjs from "dayjs";
export interface IMessage extends Chat.Message<IRawMsg> { }
export function msg_adapter(src: IRawMsg, account: string, http_addr: string): IMessage {
  src.account = account
  const { time, isSelf, seq, senderName, sender } = src
  const type = RawMsgType.readType(src);
  const content = RawMsgType.readContent(src);
  const ret: IMessage = {
    type,
    content,
    id: '' + seq,
    isSelf: !!isSelf,
    timestamp: dayjs(time).unix() * 1000,
    sender: {
      head: void 0,
      name: senderName,
      id: sender
    },
    raw: src,
  }
  if (type === Chat.MessageType.link) {
    const raw_link_info = src.contents as Unsafe<IRawMsgContent_Link>;
    ret.link = {
      name: raw_link_info?.title,
      url: raw_link_info?.url,
    }
  }
  if (type === Chat.MessageType.file) {
    const raw_file_info = src.contents as Unsafe<IRawMsgContent_File>;
    ret.file = {
      id: raw_file_info?.md5,
      type: raw_file_info?.title,
      name: raw_file_info?.title,
      size: 0,
      url: '',
      icon: icon,
    }
  }
  if (type === Chat.MessageType.img) {
    const contents = src.contents as IRawMsgContent_Image;
    if (contents?.img_url) {
      // 远端图片
      ret.content = contents?.img_url
    } else if (http_addr) {
      // 本地图片
      ret.content = `http://${http_addr}/data/${contents.path?.replace(/\\/g, '/') + '_t.dat'}`
    } else {
      ret.type = Chat.MessageType.text;
    }
  }
  return ret;
}
export function msg_adapter2(src: ApiV1WxMessagePageList_post.RespData): IMessage {
  return msg_adapter(src, '' + src.account, '');
}